/**
 * 研发流程状态机 - 模块入口
 */

import { observable, action } from 'mobx-miniprogram';
import { 
  PROJECT_STATUS, 
  PROJECT_REVIEW_SUB_STATUS, 
  DORMANT_SUB_STATUS, 
  ACTIVE_STATE,
  SUB_STATE,
  STATE_EVENTS
} from './constants';
import transitions from './transitions';
import storeModule from './store';

// 定义状态字段常量 - 仅使用level命名法
const LEVEL1_FIELD = 'level1';
const LEVEL2_FIELD = 'level2';
const LEVEL3_FIELD = 'level3';
const LEVEL4_FIELD = 'level4';

// 导出状态机Store实例
export const researchStateMachineStore = storeModule;

// 导出状态机对象（便于业务代码调用）
export const researchStateMachine = {
  // 状态常量导出
  STATUS: PROJECT_STATUS,
  SUB_STATUS: {
    PROJECT_REVIEW: PROJECT_REVIEW_SUB_STATUS,
    ACTIVE: ACTIVE_STATE,
    DORMANT: DORMANT_SUB_STATUS
  },
  EVENT: STATE_EVENTS,
  
  // 状态机字段名 - 仅使用level命名法
  FIELDS: {
    LEVEL1: LEVEL1_FIELD,
    LEVEL2: LEVEL2_FIELD,
    LEVEL3: LEVEL3_FIELD,
    LEVEL4: LEVEL4_FIELD
  },
  
  // 状态字段 - 直接导出
  LEVEL1_FIELD,
  LEVEL2_FIELD,
  LEVEL3_FIELD,
  LEVEL4_FIELD,
  
  // 获取状态名称（用于显示）
  getStatusName(status) {
    switch (status) {
      case PROJECT_STATUS.NONE: return '未定义';
      case PROJECT_STATUS.PROJECT_REVIEW: return '项目审核';
      case PROJECT_STATUS.DORMANT: return '休眠';
      case PROJECT_STATUS.ACTIVE: return '活动';
      case PROJECT_STATUS.COMPLETED: return '已完成';
      default: return '未知状态';
    }
  },
  
  // 获取子状态名称
  getSubStatusName(status, subStatus) {
    // 根据主状态选择子状态集合
    let subStatusSet = {};
    
    switch (status) {
      case PROJECT_STATUS.PROJECT_REVIEW:
        subStatusSet = PROJECT_REVIEW_SUB_STATUS;
        break;
      case PROJECT_STATUS.ACTIVE:
        subStatusSet = ACTIVE_STATE;
        break;
      case PROJECT_STATUS.DORMANT:
        subStatusSet = DORMANT_SUB_STATUS;
        break;
      case PROJECT_STATUS.COMPLETED:
        return '已完成';
      default:
        return '未知子状态';
    }
    
    // 在对应子状态集合中查找名称
    for (const key in subStatusSet) {
      if (subStatusSet[key] === subStatus) {
        return key;
      }
    }
    
    return '未知子状态';
  },
  
  // 状态机功能方法
  getAvailableEvents: transitions.getAvailableEvents,
  
  // Store方法转发
  transition: researchStateMachineStore.transition.bind(researchStateMachineStore),
  canTransition: researchStateMachineStore.canTransition.bind(researchStateMachineStore)
};

// 默认导出
export default {
  researchStateMachine,
  researchStateMachineStore
}; 